/* 
 * Copyright 2023 Sven dj.Zheng All Rights Reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"). 
 * You may not use this file except in compliance with the License. 
 * A copy of the License is located at 
 * 
 *     http://aws.amazon.com/apache2.0/ 
 * 
 * or in the "license" file accompanying this file. This file is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
 * express or implied. See the License for the specific language governing 
 * permissions and limitations under the License. 
 */

#include "JSONUtils.h"


namespace sdk {
namespace utils {
namespace cjson {
#if 0
/// String to identify log entries originating from this file.
static const std::string TAG("JsonUtils");

/**
 * Create a LogEntry using this file's TAG and the specified event string.
 *
 * @param The event string for this @c LogEntry.
 */
#define LX(event) utils::logging::LogEntry(TAG, event)

//! Type of JSON value
enum Type {
    kInvalidType = cJSON_Invalid, //!< invalide
    kFalseType = cJSON_False,     //!< false
    kTrueType = cJSON_True,      //!< true
    kNullType = cJSON_NULL,      //!< null
    kNumberType = cJSON_Number,     //!< number
    kStringType = cJSON_String,    //!< string
    kArrayType = cJSON_Array,     //!< array 
    kObjectType = cJSON_Object,    //!< object
    kRawType = cJSON_Raw		//!< raw json   
};

/**
 * Write a @c cJSON Type value to an @c ostream as a string.
 *
 * @param stream The stream to write the value to.
 * @param type The type value to write to the @c ostream as a string.
 * @return The @c ostream that was passed in and written to.
 */
static std::ostream& operator<<(std::ostream& stream, cjson::Type type) {
    switch (type) {
		case cjson::Type::kInvalidType:
			stream << "INVALID";
			break;
        case cjson::Type::kFalseType:
            stream << "FALSE";
            break;
        case cjson::Type::kTrueType:
            stream << "TRUE";
            break;
        case cjson::Type::kNullType:
            stream << "NULL";
            break;
        case cjson::Type::kNumberType:
            stream << "NUMBER";
            break;
        case cjson::Type::kStringType:
            stream << "STRING";
            break;
        case cjson::Type::kArrayType:
            stream << "ARRAY";
            break;			
        case cjson::Type::kObjectType:
            stream << "OBJECT";
            break;
		case cjson::Type::kRawType:
			stream << "RAW";
			break;

    }
    return stream;
}
#endif
/**
 * Invoke a cJSON parse a on JSON string.
 *
 * @param jsonContent The JSON content to be parsed.
 * @param[out] document The output parameter rapidjson document.
 * @return @c true If the JSON content was valid, @c false otherwise.
 */
bool parseJSON(const std::string& jsonContent, cJSON** document) {
    if (!document) {
        //AISDK_ERROR(LX("parseJSONFailed").d("reason", "nullDocument"));
        return false;
    }

    *document = cJSON_Parse(jsonContent.c_str());

    if (!*document) {
        //AISDK_ERROR(LX("parseJSONFailed")
        //                .d("offset", cJSON_GetErrorPtr()));
        return false;
    }
    return true;
}

/**
 * Serialize a 'cJSON' node, which must be of Object type, into a string.
 *
 * @param documentNode A logical node within a parsed JSON document which 'cJSON' understands.
 * @param[out] value The output parameter where the converted string will be assigned.
 */
static void serializeJSONObjectToString(const cJSON *documentNode, std::string* value) {
    if (!cJSON_IsObject(documentNode)) {
        //AISDK_ERROR(LX("serializeJSONObjectToStringFailed")
        //                .d("reason", "invalidType")
        //                .d("expectedType", cjson::Type::kObjectType)
        //                .d("type", documentNode->type));
        return;
    }
	
    *value = cJSON_Print(documentNode);
}

// Overloads of convertToValue
bool convertToValue(const cJSON *documentNode, std::string* value) {
    if (!value) {
        //AISDK_ERROR(LX("convertToStringValueFailed").d("reason", "nullValue"));
        return false;
    }

    if (!cJSON_IsString(documentNode) && !cJSON_IsObject(documentNode)) {
        //std::ostringstream expectedTypes;
        //expectedTypes << cjson::Type::kObjectType << "/" << cjson::Type::kStringType;

        //AISDK_ERROR(LX("convertToStringValueFailed")
        //                .d("reason", "invalidType")
        //                .d("expectedTypes", expectedTypes.str())
        //                .d("type", documentNode->type));
        return false;
    }

    if (cJSON_IsString(documentNode)) {		
        *value = cJSON_GetStringValue((cJSON *)documentNode);
    } else if (cJSON_IsObject(documentNode)) {
        serializeJSONObjectToString(documentNode, value);
    }

    return true;
}

bool convertToValue(const cJSON *documentNode, int64_t* value) {
    if (!value) {
        //AISDK_ERROR(LX("convertToInt64ValueFailed").d("reason", "nullValue"));
        return false;
    }

    if (!cJSON_IsNumber(documentNode)) {
        //AISDK_ERROR(LX("convertToInt64ValueFailed").d("reason", "invalidValue").d("expectedValue", "Int64"));
        return false;
    }

    *value = documentNode->valueint;

    return true;
}

}  // namespace cjson
}  // namespace utils
}  // namespace sdk
